package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ActivityDetail;
import com.mofang.powerdekorhelper.model.PastActivityList;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.ActivityDetailView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class ActivityDetailPersenter extends BasePresent<ActivityDetailView> {

    RetrofitSerives retrofitSerives;
    Call<ActivityDetail> detailCall;

    Call<PastActivityList> pastActivityListCall;

    public void getActivityDetail(Integer activityId) {

        view.showProgress();

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("id", activityId);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        final String requestStr = jsonParams.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        detailCall = retrofitSerives.getActivityDetail(body);
        detailCall.enqueue(new Callback<ActivityDetail>() {
            @Override
            public void onResponse(Call<ActivityDetail> call, Response<ActivityDetail> response) {
                if (response.isSuccessful()) {

                    if (response.body().getResult() != null) {

                        view.setActivityDetail(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ActivityDetail> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });

    }

    public void getQrCode(int type, int value, boolean ishyaline) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        String jsonParams = getParams(type, value, ishyaline);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        Call<ResultMessage4> call = retrofitSerives.getQrcode(body);

        call.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setQrCode(response.body().getResult());
                        view.hideProgress();
                    } else {
                        view.onError(Constants.GTEQRCODE_FAIL_INFO);
                        view.hideProgress();
                    }
                } else {
                    view.onError(Constants.GTEQRCODE_FAIL_INFO);
                    view.hideProgress();
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.onError(Constants.GTEQRCODE_FAIL_INFO);
                view.hideProgress();
            }
        });
    }

    public void getPastActivityList(int page, int pageSize, Integer shop_id) {

        view.showProgress();

        JSONObject jsonParams = new JSONObject();
        try {
            jsonParams.put("page", page);
            jsonParams.put("pageSize", pageSize);
            JSONObject param = new JSONObject();
            param.put("shop_id", shop_id);
            jsonParams.put("param", param);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String requestStr = jsonParams.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);

        pastActivityListCall = retrofitSerives.getPastActivityList(body);

        pastActivityListCall.enqueue(new Callback<PastActivityList>() {
            @Override
            public void onResponse(Call<PastActivityList> call, Response<PastActivityList> response) {

                if (response.isSuccessful()) {

                    if (response.body() != null) {
                        if (response.body().getData().size() > 0) {
                            view.setPastActivityList(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<PastActivityList> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    private String getParams(int type, int value, boolean ishyaline) {
        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("type", type);
            paramsJson.put("value", value);
            paramsJson.put("ishyaline", ishyaline);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }

    public void stopRequest() {

        if (detailCall != null) {
            detailCall.cancel();
        }
    }


}
